package com.woniuxy.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateConverter
 * @Description 时间类型参数转换器
 * @Author 王昊波
 * @Version 1.0
 */
public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd");
        Date date=null;
        if(s.contains("/")){
            try {
                date=format2.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if(s.contains("-")){
            try {
                date=format1.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }
}
